package com.xunlai.infra.permission.manager.web.controller;

import com.xunlai.infra.permission.common.exception.PermissionBusinessException;
import com.xunlai.infra.permission.manager.web.service.RoleService;
import com.xunlai.infra.permission.manager.web.vo.*;
import com.xunlai.infra.permission.manager.web.vo.role.*;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liang
 * @date 12/9/22 9:42 AM
 */
@RestController
@PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
@RequestMapping("system/")
public class RoleController {

    private RoleService roleService;

    @PostMapping("role/create")
    public BaseResponse<Long> createRole(@RequestBody @Validated(CreateRoleReqVo.ValidRoleCreateGroup.class) CreateRoleReqVo createRoleReqVo) {
        try {
            return BaseResponse.success(roleService.createRole(createRoleReqVo), "SUCCESS");
        } catch (PermissionBusinessException e) {
            return BaseResponse.error(e.getCode(), null, e.getMsg());
        }
    }


    @PostMapping
    @RequestMapping("role/{roleId}/relation/create")
    public BaseResponse<Void> createRoleRelation(@PathVariable("roleId") Long roleId, @RequestBody @Valid List<CreateRoleRelationVo> createRoleRelationVos) {

        try {
            roleService.createRelation(roleId, createRoleRelationVos);
            return BaseResponse.success(null, "SUCCESS");
        } catch (PermissionBusinessException e) {
            return BaseResponse.error(e.getCode(), null, e.getMsg());
        }
    }


    @PostMapping("role/{roleId}/permission/create")
    public BaseResponse<Void> createRolePermission(@PathVariable("roleId") Long roleId, @RequestBody @Valid List<CreateRolePermissionReqVo> createRolePermissionReqVos) {

        try {
            roleService.createPermission(roleId, createRolePermissionReqVos);
            return BaseResponse.success(null, "SUCCESS");
        } catch (PermissionBusinessException e) {
            return BaseResponse.error(e.getCode(), null, e.getMsg());
        }
    }

    @PostMapping("role/switchEnable")
    public BaseResponse<Void> switchRoleEnable(@RequestBody @Validated(CreateRoleReqVo.ValidSwitchRoleEnableGroup.class) UpdateRoleReqVo updateRoleReqVo) {
        try {
            roleService.switchEnable(updateRoleReqVo.getId(), updateRoleReqVo.getEnable());
            return BaseResponse.success(null, "SUCCESS");
        } catch (PermissionBusinessException e) {
            return BaseResponse.error(e.getCode(), null, e.getMsg());
        }
    }

    @PostMapping("role/changeName")
    public BaseResponse<Void> changeRoleName(@RequestBody @Validated(CreateRoleReqVo.ValidChangeRoleNameGroup.class) UpdateRoleReqVo updateRoleReqVo) {
        try {
            roleService.changeRoleName(updateRoleReqVo.getId(), updateRoleReqVo.getRoleName());
            return BaseResponse.success(null, "SUCCESS");
        } catch (PermissionBusinessException e) {
            return BaseResponse.error(e.getCode(), null, e.getMsg());
        }
    }

    @DeleteMapping("role/{roleId}")
    public BaseResponse<Boolean> deleteRole(@PathVariable Long roleId) {
        try {
            return BaseResponse.success(roleService.deleteRole(roleId), "SUCCESS");
        } catch (PermissionBusinessException e) {
            return BaseResponse.error(e.getCode(), null, e.getMsg());
        }
    }

    @GetMapping("role/list")
    public BaseResponse<BasePageRespVo<RoleListVo>> listRole(QueryRoleReqVo queryRoleReqVo,
                                                             @RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                                             @RequestParam(required = false,defaultValue = "10") Integer pageSize) {
        return BaseResponse.success(roleService.pageListRole(queryRoleReqVo,pageNum,pageSize), "SUCCESS");
    }

    @Autowired
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }
}
